<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Student extends MX_Controller {
	
	protected $id = 0;
	
	protected $record_per_page = 0;
	
	protected $validation;
	
	public function __construct()
    {
        parent::__construct();
		$this->record_per_page = $this->system_library->settings['record_per_page'];
		$this->load->library('users/auth');
		$this->load->library('form_validation');
		$this->load->library('pagination');
        $this->form_validation->CI =& $this;
		
		$this->lang->load('student/student');
		
		$this->load->model('student_m', 'student');
		
		$this->template
				->set_theme('default')
				->set_layout('default')
				->append_metadata($this->asset->css('admin/default.css'))
				->append_metadata($this->asset->css('admin/app.css'))
				->append_metadata($this->asset->css('notifcation.slider.css'))
				->append_metadata($this->asset->css('admin/jquery.accordion.css'))
				->append_metadata($this->asset->css('reset.css'))
				->append_metadata($this->asset->css('general.css'))
				->append_metadata($this->asset->js('jquery/jquery-1.6.2.min.js'))
				->append_metadata($this->asset->js('jquery_accordion_init.js'))
				->append_metadata($this->asset->js('jquery.freeow.js'))
				->append_metadata($this->asset->js('notificationBox.jquery.js'))
				->append_metadata($this->asset->css('admin/usual.tab.css'))
				->append_metadata($this->asset->js('jquery.idTabs.min.js'))
				->set_partial('header', 'partials/header')
				->set_partial('breadcrumb','partials/breadcrumb')
                ->set_partial('footer', 'partials/footer')
				->set_partial('navigation', 'partials/admin/menu')
				->set_breadcrumb('Home', 'admin/dashboard');
				
    }
	
	public function index()
	{
		$this->lists();
	}
	
	public function lists($page = 0)
	{
		$student = new Student_m();
		
		if (!$this->auth->is_logged_in() === TRUE)
		{
			redirect('users/login', 'refresh');
		}
		
		$this->load->library('pagination');
		
		if ($page < 0  || strlen($page) > 13)
		{
			redirect('student/lists');
		}
		else
		{
			$limit = $this->record_per_page;
			
			$total_rows = $student->get_active_student_count(($limit + $page), $page);
			
			if($total_rows < $page)
			{
				redirect('users/lists');
			}
			else
			{
				$data['students']			= $student->get_all_active_student($limit, $page);
				
				//setup pagination
				$config['base_url']			= site_url('student/lists/');
				$config['per_page']			= $limit;
				$config['uri_segment'] 		= 4;
				$config['total_rows']		= $total_rows;
				$config['anchor_class'] 	= 'class="btn"';
				$config['cur_tag_open'] 	= '<div class="btn btn-disabled">';
				$config['cur_tag_close'] 	= '</div>';
				
				$this->pagination->initialize($config);
				
				$data['page'] 				= $this->pagination->create_links();
				
				$this->template
							->append_metadata($this->asset->css('facebox.css'))
							->append_metadata($this->asset->js('jquery/facebox.js'))
							->set_breadcrumb('Student','')
							->build('manage/list', $data);
			}
		}
	}
	
	public function create()
	{	
		$this->form_validation->set_rules($this->validation);
		
		if ($this->form_validation->run())
		{
			$this->student->create_student();
			
			$this->session->set_flashdata('msg_title',lang('student_success_title'));
			$this->session->set_flashdata('msg',lang('success_success_insert'));
			redirect('student/student/');
		}
		
		if (isAjax())
		{
			$this->load->view('manage/create',$data);
		}
		else
		{
			$data['page_label'] = 'NEW STUDENT';
			$this->template
						->append_metadata($this->asset->js('jquery.tab.init.js'))
						->append_metadata($this->asset->css('admin/jquery.tab.css'))
						->set_breadcrumb('Student','student/lists')
						->set_breadcrumb('New')
						->build('manage/create',$data);
		}
	}
	
	public function view($id = null)
	{
		$this->lang->load('student/student');
		$this->load->model('transaction/transaction_m', 'transaction');
		
		$data['invoices'] =  $this->transaction->get_all_invoice_header($id);
		$data['profile'] = $this->student->get_student_profile($id);
		
		//foreach($student AS $profile)
		//{
		//	$user_data['id'] 			= $profile->id;
		//	$user_data['firstname'] 	= $profile->firstname;
		//	$user_data['lastname'] 		= $profile->lastname;
		//	$user_data['phone']			= $profile->phone;
		//	$user_data['mobile']		= $profile->mobile;
		//	$user_data['email']			= $profile->email;
		//	$user_data['middlename'] 	= $profile->middlename;
		//	$user_data['nationality'] 	= $profile->nationality;
		//	$user_data['religion'] 		= $profile->religion;
		//}
		//
		//$data['profile'] = $user_data;
		
		$data['page_label'] = 'STUDENT';
		
		$this->template
					->append_metadata($this->asset->js('jquery.tab.init.js'))
					->append_metadata($this->asset->css('admin/jquery.tab.css'))
					->set_breadcrumb('Student','student/lists')
					->set_breadcrumb('')
					->build('manage/view', $data);
	}
	
	public function get_items()
	{
		$query = $this->input->get('q');
		
		$item = new Item_m();
		
		$item->like('name',$query);
		$item->or_like('description',$query);
		$item->where('is_active',1);
		$item->get();
		
		$data = array();
		
		foreach( $item as $i )
		{
			$data[] = array(
				'name'	=> $i->name,
				'id'	=> $i->id
			);
		}
		
		echo json_encode($data);
	}
	
	public function get_student()
	{
		$query = $this->input->get('q');
		
		$student = new Student_m();
		
		$student->like('firstname',$query);
		$student->or_like('lastname',$query);
		$student->or_like('middlename',$query);
		$student->or_like('number',$query);
		$student->get();
		
		$data = array();
		
		foreach( $student as $s )
		{
			$data[] = array(
				'name'	=> $s->number. ' : ' .$s->firstname.' '.$s->middlename.' '.$s->lastname,
				'id'		=> $s->id
			);
		}
		
		echo json_encode($data);
		
	}
	
	public function _check_student_number($str)
	{
		$student = new Student_m();
		
		if ( $student->check_student_number($str) )
		{
			return true;
		}
		else
		{
			$this->form_validation->set_message('_check_student_number', lang('student_number_exist'));
			return false;
		}
		
		
	}
}